<?php

/**
 * usuario actions.
 *
 * @package    catalogo
 * @subpackage usuario
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class usuarioActions extends sfActions
{


  public function executeShow(sfWebRequest $request)
  {
    $this->usuario = Doctrine::getTable('Usuario')->find(array($request->getParameter('id')));
    $this->forward404Unless($this->usuario);
  }

  public function executeNew(sfWebRequest $request)
  {
    $this->form = new UsuarioForm();    
  }

  public function executeCreate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST));
    
    if ($usuario = Doctrine::getTable('Usuario')->findOneBy('email', $request->getPostParameter('usuario[email]')))
    {
      //$usuario = Doctrine::getTable('Usuario')->find(array($usuario->getId()));
      $this->form = new UsuarioForm($usuario);
    }
    else
      $this->form = new UsuarioForm();

    $this->processForm($request, $this->form);

    $this->setTemplate('new');
  }

  public function executeEdit(sfWebRequest $request)
  {
    $this->forward404Unless($usuario = Doctrine::getTable('Usuario')->find(array($request->getParameter('id'))), sprintf('Object usuario does not exist (%s).', $request->getParameter('id')));
    $this->form = new UsuarioForm($usuario);
  }

  public function executeUpdate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
    $this->forward404Unless($usuario = Doctrine::getTable('Usuario')->find(array($request->getParameter('id'))), sprintf('Object usuario does not exist (%s).', $request->getParameter('id')));
    $this->form = new UsuarioForm($usuario);

    $this->processForm($request, $this->form);

    $this->setTemplate('edit');
  }

  public function executeDelete(sfWebRequest $request)
  {
    $request->checkCSRFProtection();

    $this->forward404Unless($usuario = Doctrine::getTable('Usuario')->find(array($request->getParameter('id'))), sprintf('Object usuario does not exist (%s).', $request->getParameter('id')));
    $usuario->delete();

    $this->redirect('usuario/index');
  }

  protected function processForm(sfWebRequest $request, sfForm $form)
  {	
    
  	if (sfConfig::get('app_recaptcha_active'))
    {
	  	$captcha = array('recaptcha_challenge_field' => $request->getParameter('recaptcha_challenge_field'),
	                     'recaptcha_response_field'  => $request->getParameter('recaptcha_response_field'));
	                     
      if ($form->getObject()->getId()) //el usuario ya existe entonces le asocia el id existente
      {
        $current_user = array('id' => $form->getObject()->getId());
        
        $form->bind( 
          array_merge( 
            $request->getParameter($form->getName()), 
            array('captcha' => $captcha), 
            $current_user), 
          $request->getFiles($form->getName()));
      }
      else
      {
        $form->bind( 
          array_merge( 
            $request->getParameter($form->getName()), 
            array('captcha' => $captcha)), 
          $request->getFiles($form->getName()));
      }
    } 
    else 
    {
      if ($form->getObject()->getId()) //el usuario ya existe entonces le asocia el id existente
      {
        $current_user = array('id' => $form->getObject()->getId());
                
        $form->bind(
          array_merge(
            $request->getParameter($form->getName()), 
            $current_user), 
          $request->getFiles($form->getName()));
      }
      else
      {
        $form->bind(
          $request->getParameter($form->getName()), 
          $request->getFiles($form->getName()));
      }
    }  	

    if ($form->isValid())
    {
      //die($form->getObject()->getNombre());
      $usuario  = $form->save();
      $articulo = $form->getEmbeddedForm('articulo')->getObject();
      
      $this->redirect('ficha_aviso', $articulo);
    }
  }  
}
